package com.example.springbootoauth2.controller;

import com.example.springbootoauth2.config.security.CustomUserDetails;
import com.example.springbootoauth2.model.commons.CallbackResult;
import com.example.springbootoauth2.model.user.pojo.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;

public class BaseController {

    protected Log log = LogFactory.getLog(this.getClass());

    public CallbackResult success(String msg, Object obj) {
        CallbackResult cr = new CallbackResult(true, msg);
        if(log.isDebugEnabled()) {
            log.debug("Object ---------- " + obj);
        }
        cr.setDetails(obj);
        return cr;
    }

    public CallbackResult error(String msg, Exception e) {
        CallbackResult cr = new CallbackResult(false, msg);
        log.error("--------------- Exception ---------------", e);
        return cr;
    }

    public User getUser() {
        CustomUserDetails customUserDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return customUserDetails.getUser();
    }

}
